package com.lineng.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lineng.pojo.Activity;
import com.lineng.service.ActivityService;
import com.lineng.util.DateUtils;

@Controller
public class ActivityController {

	@Autowired
	ActivityService activityService;

	/**
	 * 获取党建活动内容
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getActivity", method = RequestMethod.GET)
	@ResponseBody
	public List<Activity> getActivity() {
		List<Activity> activities = activityService.getActivity();
		return activities;

	}

	@RequestMapping(value = "/getActivityByDate", method = RequestMethod.POST, produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String getActivityByData(@DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
			@DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
		return JSON.toJSONString(activityService.getActivityByDate(startDate, DateUtils.getNextDay(endDate)));
	}

	/**
	 * 添加党建活动内容
	 * 
	 * @param activity
	 * @return
	 */
	@RequestMapping(value = "/insertActivity", method = RequestMethod.POST, produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String insertMetting(Activity activity, HttpServletRequest request) {
		return JSON.toJSONString(
				activityService.insertActivity(activity, (String) request.getSession().getAttribute("username")));

	}

	/**
	 * 更新党建活动内容
	 * 
	 * @param activity
	 * @return
	 */
	@RequestMapping(value = "/updateActivity", method = RequestMethod.POST, produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String updateMetting(Activity activity, HttpServletRequest request) {
		return JSON.toJSONString(
				activityService.updateActivity(activity, (String) request.getSession().getAttribute("username")));

	}

	/**
	 * 删除党建活动内容
	 * 
	 * @param id
	 * @return
	 */

	@RequestMapping(value = "/deleteActivity", method = RequestMethod.POST, produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String deleteMetting(Integer[] idList) {
		return JSON.toJSONString(activityService.deleteActivity(idList));
	}

}
